﻿
Imports System.IO
Imports System.IO.File
Imports System.Xml
Imports System.Net

Public Class FrmMachine

    Private Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click
        If Button5.Text = "修改" Then
            DeviceType.ReadOnly = False
            Manufacturer.ReadOnly = False
            DeviceCode.ReadOnly = False
            DeviceId.ReadOnly = False
            Line.ReadOnly = False
            IP.ReadOnly = False
            StateFre.ReadOnly = False
            ProductFre.ReadOnly = False
            Family.ReadOnly = False
            Station.ReadOnly = False
            Fixture.ReadOnly = False
            Button5.Text = "保存"
        ElseIf Button5.Text = "保存" Then
            SaveData()
            machineInfo.Device_type = DeviceType.Text
            machineInfo.Manufacturer = Manufacturer.Text
            machineInfo.Device_Code = DeviceCode.Text
            machineInfo.Device_id = DeviceId.Text
            machineInfo.Line = Line.Text
            machineInfo.IP = IP.Text
            machineInfo.Fixture = Fixture.Text
            machineInfo.Station = Station.Text
            StateFreTime = StateFre.Text
            ProductFreTime = ProductFre.Text
            SNInfo.Family = Family.Text
            Dim Machine As New XmlTextWriter(Application.StartupPath & "\MachineInfo.xml", System.Text.Encoding.GetEncoding("utf-8"))
            Try
                Machine.WriteStartDocument()
                Machine.Formatting = Formatting.Indented
                Machine.WriteStartElement("MachineInfo")
                Machine.WriteElementString("DeviceType", machineInfo.Device_type)
                Machine.WriteElementString("Manufacturer", Manufacturer.Text)
                Machine.WriteElementString("DeviceCode", DeviceCode.Text)
                Machine.WriteElementString("DeviceId", DeviceId.Text)
                Machine.WriteElementString("Line", Line.Text)
                Machine.WriteElementString("IP", IP.Text)
                Machine.WriteElementString("Station", Station.Text)
                Machine.WriteElementString("Fixture", Fixture.Text)
                Machine.WriteElementString("StateFre", StateFreTime)
                Machine.WriteElementString("ProductFre", ProductFreTime)
                Machine.WriteElementString("Family", Family.Text)
                Machine.WriteEndElement()
                DeviceType.ReadOnly = True
                Manufacturer.ReadOnly = True
                DeviceCode.ReadOnly = True
                DeviceId.ReadOnly = True
                Line.ReadOnly = True
                IP.ReadOnly = True
                Fixture.ReadOnly = True
                Station.ReadOnly = True
                StateFre.ReadOnly = True
                ProductFre.ReadOnly = True
                Family.ReadOnly = True
                Button5.Text = "修改"
                FrmMain.UploadStatus.Interval = StateFreTime * 1000
                'FrmMain.UploadMainInfo.Interval = ProductFreTime * 1000
                MsgBox("设备信息保存成功！")
            Catch ex As Exception
                MsgBox("设备信息保存失败!" & ex.Message.ToString)
            Finally
                Machine.Close()
                FrmMain.InitMachineInfo()
            End Try
        End If

    End Sub

    Private Sub FrmMachine_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Dim xmlpath = Application.StartupPath & "\MachineInfo.xml"
        If Exists(xmlpath) Then
            DeviceType.Text = machineInfo.Device_type
            Manufacturer.Text = machineInfo.Manufacturer
            DeviceCode.Text = machineInfo.Device_Code
            DeviceId.Text = machineInfo.Device_id
            Line.Text = machineInfo.Line
            IP.Text = machineInfo.IP
            Fixture.Text = machineInfo.Fixture
            Station.Text = machineInfo.Station
            StateFre.Text = StateFreTime
            ProductFre.Text = ProductFreTime
            Family.Text = SNInfo.Family
        End If
    End Sub

    Private Sub IP_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles IP.KeyPress
        If Char.IsDigit(e.KeyChar) OrElse e.KeyChar = Chr(8) OrElse e.KeyChar = "." Then
            e.Handled = False
        Else
            e.Handled = True
            MsgBox("只能输入数值或'.'")
        End If
    End Sub
    Private Sub SaveData()
        Dim xmlpath = Application.StartupPath & "\数据上传程序\Param.xml"
        Dim doc As XmlDocument = New XmlDocument
        doc.Load(xmlpath)    '加载Xml文件  
        Dim rootElem As XmlElement = doc.DocumentElement
        Dim nodeList As XmlNodeList = rootElem.ChildNodes
        For Each xn In nodeList '遍历所有子节点 
            Dim xe As XmlElement = CType(xn, XmlElement) '将子节点类型转换为XmlElement类型 
            If xe.Name = "MachineStatusUploadPath" Then
                xe.InnerText = "Share/" & Line.Text & "/" & DeviceCode.Text & "/MachineStatus/"
            ElseIf xe.Name = "MainInfoUploadPath" Then
                xe.InnerText = "Share/" & Line.Text & "/" & DeviceCode.Text & "/MainInfo/"
            ElseIf xe.Name = "SNInfoUploadPath" Then
                xe.InnerText = "Share/" & Line.Text & "/" & DeviceCode.Text & "/SNInfo/"
            ElseIf xe.Name = "SNDetailUploadPath" Then
                xe.InnerText = "Share/" & Line.Text & "/" & DeviceCode.Text & "/SNDetail/"
            End If
        Next
        doc.Save(xmlpath)
        'Dim MachineData As New XmlTextWriter(Application.StartupPath & "\数据上传程序\Param.xml", System.Text.Encoding.GetEncoding("utf-8"))
        'MachineData.WriteStartDocument()
        'MachineData.Formatting = Formatting.Indented
        'MachineData.WriteStartElement("Param")
        'MachineData.setattribute("MachineStatusUploadPath", "Share/FA_" & Line.Text & "/" & DeviceCode.Text & "/MachineStatus/")
        'MachineData.WriteElementString("MainInfoUploadPath", "Share/FA_" & Line.Text & "/" & DeviceCode.Text & "/MainInfo/")
        'MachineData.WriteElementString("SNInfoUploadPath", "Share/FA_" & Line.Text & "/" & DeviceCode.Text & "/SNInfo/")
        'MachineData.WriteElementString("SNDetailUploadPath", "Share/FA_" & Line.Text & "/" & DeviceCode.Text & "/SNDetail/")
        'MachineData.WriteEndElement()
        'MachineData.Close()
    End Sub
End Class